<template>
  <div class="monitor-abnorm-chart">
    <el-row :gutter="20">
      <el-col :span="10">
        <el-card>
          <tag-content-header>
            <template #title>
              <span class="title-text">本次各类目状态分布情况</span>
            </template>
          </tag-content-header>
          <div class="abnorm-chart-wrapper" id="abnorm-data"></div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card>
          <tag-content-header>
            <template #title>
              <span class="title-text">剩余未处理预警项</span>
            </template>
          </tag-content-header>
          <div class="abnorm-chart-wrapper abnorm-wrapper">
            <div class="abnorm-count">
              <icon-svg name="iconAbnorm"></icon-svg><br />
              <span>{{ surplusUnprocessed }}</span>
              <br />
              未处理预警
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="8">
        <el-card>
          <tag-content-header>
            <template #title>
              <span class="title-text">启用/停用数</span>
            </template>
          </tag-content-header>
          <div class="abnorm-chart-wrapper abnorm-wrapper">
            <div class="abnorm-count begin-count">
              <icon-svg name="iconMonitorPlay"></icon-svg><br />
              <span>{{ startNum }}</span>
              <br />
              启用数
            </div>
            <div class="abnorm-count stop-count">
              <icon-svg name="iconMonitorStop"></icon-svg><br />
              <span>{{ stopNum }}</span>
              <br />
              停用数
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import * as echarts from 'echarts'
export default {
  data() {
    return {
      surplusUnprocessed: 0,
      good: 0,
      excellent: 0,
      earlyWarning: 0,
      startNum: 0,
      stopNum: 0,
      scoreChart: null
    }
  },
  methods: {
    getCount() {
      this.$ajax({
        url: this.$ajax.setUrl('tag/tagExceptionWarning/getCategoryDistributionStatus'),
        method: 'get',
        params: this.$ajax.setParams()
      }).then(({ data }) => {
        if (data && data.code === '0') {
          this.surplusUnprocessed = data.data.surplusUnprocessed
          this.good = data.data.good
          this.excellent = data.data.excellent
          this.earlyWarning = data.data.earlyWarning
          this.startNum = data.data.startNum
          this.stopNum = data.data.stopNum
          this.initChart()
        } else {
          this.$message.error(data.data || data.msg)
        }
      })
    },
    initChart() {
      if (!this.scoreChart) this.scoreChart = echarts.init(document.getElementById('abnorm-data'))
      let option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        grid: {
          bottom: 20,
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            data: ['优秀', '良好', '预警'],
            axisLine: {
              show: true,
              lineStyle: {
                color: '#ccc'
              }
            },
            axisTick: {
              alignWithLabel: true
            },
            axisLabel: {
              color: '#666'
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            axisLine: {
              show: true,
              lineStyle: {
                color: '#ccc'
              }
            },
            axisLabel: {
              color: '#666'
            },
            splitLine: {
              lineStyle: {
                type: 'dashed',
                color: '#eee'
              }
            }
          }
        ],
        series: [
          {
            type: 'bar',
            barWidth: '30%',
            itemStyle: {
              borderRadius: [3, 3, 0, 0],
              color: params => {
                return new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: params.dataIndex === 0 ? '#45bf7b' : params.dataIndex === 1 ? '#3ab2d0' : '#f1754e'
                  },
                  {
                    offset: 1,
                    color: params.dataIndex === 0 ? '#65d496' : params.dataIndex === 1 ? '#54c6e2' : '#f98c6a'
                  }
                ])
              }
            },
            label: {
              normal: {
                show: true,
                position: 'top',
                fontSize: 17,
                fontWeight: 'bold'
              }
            },
            data: [this.excellent, this.good, this.earlyWarning]
          }
        ]
      }
      this.scoreChart.setOption(option, true)
      window.addEventListener('resize', () => {
        this.scoreChart.resize()
      })
    }
  }
}
</script>

<style lang="scss">
.monitor-abnorm-chart {
  margin-bottom: 20px;
  .tag-content-header {
    padding-left: 25px;
    margin-bottom: 0;
  }
  .el-card__body {
    padding: 0;
    position: relative;
  }
  .abnorm-chart-wrapper {
    height: 300px;
  }
  .monitor-chart-title {
    padding-left: 25px;
    line-height: 40px;
    position: absolute;
    top: 0;
    left: 0;
    &::before {
      width: 5px;
      content: '';
      background-color: #54c6e2;
      position: absolute;
      top: 12px;
      left: 10px;
      bottom: 12px;
    }
  }
  .abnorm-wrapper {
    display: flex;
    justify-content: space-evenly;
    align-items: center;
  }
  .abnorm-count {
    width: 200px;
    height: 200px;
    color: #fff;
    background-color: #f1754e;
    text-align: center;
    overflow: hidden;
    border-radius: 5px;
    svg {
      width: 30px;
      height: 30px;
      margin-top: 50px;
    }
    i {
      font-size: 30px;
      margin-top: 50px;
    }
    span {
      display: inline-block;
      font-size: 35px;
      margin: 10px 0 5px;
    }
  }
  .begin-count {
    background-color: #65d496;
  }
  .stop-count {
    background-color: #54c6e2;
  }
}
</style>
